package com.company.project.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.company.project.common.utils.DataResult;
import com.company.project.common.utils.Func;
import com.company.project.entity.*;
import com.company.project.mapper.*;
import com.company.project.service.HttpSessionService;
import com.company.project.service.RoleService;
import com.company.project.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.company.project.service.TThjlmxService;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("tThjlmxService")
public class TThjlmxServiceImpl extends ServiceImpl<TThjlmxMapper, TThjlmxEntity> implements TThjlmxService {
    @Autowired
    public TThjlmxMapper thjlmxMapper;
    @Autowired
    public TCallbakLogMapper tCallbakLogMapper;
    @Resource
    private UserService userService;
    @Resource
    private SysDeptMapper sysDeptMapper;
    @Resource
    private HttpSessionService httpSessionService;
    @Resource
    private RoleService roleService;
    @Resource
    private TWdkhMapper wdkhMapper;
    @Resource
    private TGzszMapper gzszMapper;
    @Resource
    private TSeatsMapper tSeatsMapper;

    @Override
    public DataResult infoData() {
        try {
            String userId = httpSessionService.getCurrentUserId();
            SysUser sysUser = userService.getById(userId);
            SysDept sysDept = sysDeptMapper.selectOne(Wrappers.<SysDept>lambdaQuery()
                    .eq(SysDept::getId, sysUser.getDeptId()));
            Map<String, Object> map = new HashMap<>();
            String roleName = roleService.getRoleNames(userId).get(0);
            List<String> dept = new ArrayList<>();
            if (!Func.equals("代理商",roleName))  dept = thjlmxMapper.selectOrg(sysDept.getDeptNo());
            //请选择人员、请选择部门;
            List<String> ygxm = wdkhMapper.selectUsername(sysDept.getDeptNo(),sysUser);
//            List<String> ygxm = thjlmxMapper.selectRy();
            TSeatsEntity tSeatsEntity = tSeatsMapper.selectOne(Wrappers.<TSeatsEntity>lambdaQuery().eq(TSeatsEntity::getAccountid, sysUser.getId()));

            TGzszEntity tGzszEntity = gzszMapper.selectOne(Wrappers.<TGzszEntity>lambdaQuery().eq(TGzszEntity::getSsgs, tSeatsEntity.getCorporatename()));
            //是否隐藏
            List<SysRole> roleInfoByUserId = roleService.getRoleInfoByUserId(userId);
            List<String> collect = roleInfoByUserId.stream().map(e -> e.getName()).collect(Collectors.toList());
            if (Func.isNotEmpty(tGzszEntity)) {
                if (collect.contains("员工") || collect.contains("主管")) {
                    map.put("isyc", tGzszEntity.getIsyc());
                }
            }
            map.put("ygxm",ygxm);
            map.put("roleName",roleName);
            map.put("dept",dept);
            return DataResult.success(map);
        } catch (Exception e) {
            e.printStackTrace();
            return DataResult.fail("初始化选项失败，请联系管理员！");
        }
    }

    @Override
    public DataResult listByPage(Page page, TThjlmxEntity tThjlmx) {
        IPage<TThjlmxEntity> iPage = null;
        //通过access_token拿userId
        String userId = httpSessionService.getCurrentUserId();
        SysUser sysUser = userService.getById(userId);
        SysDept sysDept = sysDeptMapper.selectOne(Wrappers.<SysDept>lambdaQuery()
                .eq(SysDept::getId, sysUser.getDeptId()));
        String roleName = roleService.getRoleNames(userId).get(0);
        if (!Func.equals("代理商",roleName))  tThjlmx.setDeptNo(sysDept.getDeptNo());

        iPage = thjlmxMapper.selectPageList(page,tThjlmx,roleName,sysUser);
        return DataResult.success(iPage);
    }

    @Override
    public DataResult test() {

        //查询没有状态有录音的通话记录明细
        List<TThjlmxEntity> listNoStart = thjlmxMapper.selectNoStart();

        //处理好的数据
        List<TThjlmxEntity> listStart = new ArrayList<>();
        List<String> listBindId = new ArrayList<>();

        if (listNoStart != null && listNoStart.size() > 0) {
            List<TCallbakLogEntity> listNoStartLog = tCallbakLogMapper.selectListByNoStart();
            for(TThjlmxEntity tThjlmxEntity : listNoStart)  {
                for(TCallbakLogEntity tCallbakLogEntity : listNoStartLog) {
                    if(tThjlmxEntity.getBindId().equals(tCallbakLogEntity.getBindId())) {
                        tThjlmxEntity.setStart(tCallbakLogEntity.getStatus());
                        tThjlmxEntity.setZjjttime(tCallbakLogEntity.getStartTime());
                        tThjlmxEntity.setBjjttime(tCallbakLogEntity.getCallOutStartTime());
                        tThjlmxEntity.setEndtime(tCallbakLogEntity.getEndTime());
                        tThjlmxEntity.setZjhm(tCallbakLogEntity.getZjhm());
                        tThjlmxEntity.setBjhm(tCallbakLogEntity.getBjhm());
                        tThjlmxEntity.setXh(tCallbakLogEntity.getZjh());
                        tThjlmxEntity.setThsc(tCallbakLogEntity.getDuration());

                        tThjlmxEntity.setStartinfo(tCallbakLogEntity.getUlFailReason());

                        listStart.add(tThjlmxEntity);
                        listBindId.add(tThjlmxEntity.getBindId());
                    }
                }
            }

            if(listBindId.size()>0) {
                //删除没有状态数据
                thjlmxMapper.deleteListById(listStart);
                //插入新数据
                thjlmxMapper.insertList(listStart);
            }
        }

        return DataResult.success("处理完成");
    }
}